package net.shopxx.controller.admin;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import net.shopxx.Filter;
import net.shopxx.Filter.Operator;
import net.shopxx.Message;
import net.shopxx.constant.AccountsConstant;
import net.shopxx.entity.ShuaItem;
import net.shopxx.pager.AccountsPageable;
import net.shopxx.pager.Page;
import net.shopxx.service.AdminService;
import net.shopxx.service.ShuaService;

@Controller("adminShuaController")
@RequestMapping("/admin/shua")
public class ShuaController extends BaseController {

	@Resource(name = "shuaServiceImpl")
	private ShuaService shuaService;

	@Resource(name = "adminServiceImpl")
	private AdminService adminService;

	@RequestMapping(value = "/judge", method = RequestMethod.GET)
	public String judge(ShuaItem shua, ModelMap model) {
		model.addAttribute("item", shua);
		
		if (shua.getName() == null && shua.getQq() ==null) {
			return "/admin/shua/judge"; 
		}
		
		if(shua.getName() == null || shua.getQq() ==null){
			model.addAttribute("errorMsg", "昵称和QQ必须全部输入！");
			return "/admin/shua/judge"; 
		}
		
		if(shua.getStoreId() == null){
			model.addAttribute("errorMsg", "请输入店铺名称！");
			return "/admin/shua/judge"; 
		}

		// 判断该条信息是否可以被录入
		Boolean canSave = shuaService.judgeSave(shua);
		if (!canSave) {
			model.addAttribute("canSave", false);
		}else{
			model.addAttribute("canSave", true);
		}
		return "/admin/shua/judge";
	}
	
	/**
	 * 列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(AccountsPageable pageable, ModelMap model) {
		List<Filter> filters = new ArrayList<Filter>();

		if (StringUtils.isNotBlank(pageable.getStoreId())) {
			Filter filter = new Filter();
			filter.setProperty(AccountsConstant.ANALYZED_STORE_ID_PROPERTY);
			filter.setOperator(Operator.eq);
			filter.setValue(pageable.getStoreId());
			filters.add(filter);
		}

		if (StringUtils.isNotBlank(pageable.getOrderStatus())) {
			Filter filter = new Filter();
			filter.setProperty(AccountsConstant.ANALYZED_ORDER_STATUS_PROPERTY);
			filter.setOperator(Operator.eq);
			filter.setValue(pageable.getOrderStatus());
			filters.add(filter);
		}
		
		if(StringUtils.isNotBlank(pageable.getOperator())){
			Filter filter = new Filter();
			filter.setProperty("operator");
			filter.setOperator(Operator.eq);
			filter.setValue(pageable.getOperator());
			filters.add(filter);
		}
		
		if(pageable.getAttention()!=null && pageable.getAttention()){
			Filter filter = new Filter();
			filter.setProperty("attention");
			filter.setOperator(Operator.eq);
			filter.setValue(AccountsConstant.SHUADAN_ATTENTION_VALUE);
			filters.add(filter);
		}

		pageable.setFilters(filters);

		if (shuaService.getEditPermission()) {
			model.addAttribute("editPermission", "ok");
		}

		Page<ShuaItem> ai = shuaService.findPage(pageable);
		String operator = adminService.getCurrentUsername();
		model.addAttribute("operator", operator);
		model.addAttribute("page", ai);
		return "/admin/shua/list";
	}

	/**
	 * 统计
	 */
	@RequestMapping(value = "/statistics", method = RequestMethod.GET)
	public String statistics(AccountsPageable pageable, ModelMap model) {
		Page<ShuaItem> ai = shuaService.findStatsPage(pageable);
		model.addAttribute("page", ai);
		model.addAttribute("label", false);

		Float sum = shuaService.findStatsSum(pageable);
		Float commission = shuaService.findCommissionSum(pageable);
		Long count = shuaService.findStatsCount(pageable);
		
		if (sum != null) {
			BigDecimal s = new BigDecimal(sum);
			model.addAttribute("sum", s.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
		} else {
			model.addAttribute("sum", 0);
		}
		
		if (commission != null) {
			BigDecimal s = new BigDecimal(commission);
			model.addAttribute("commission", s.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
		} else {
			model.addAttribute("commission", 0);
		}
		model.addAttribute("count", count);

		List<String> admins = adminService.getAllAdminNames();
		model.addAttribute("admins", admins);

		return "/admin/accounts/statistics";
	}

	/**
	 * 添加
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap model) {
		return "/admin/shua/add";
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(ShuaItem shua, RedirectAttributes redirectAttributes) {
		if (!isValid(shua)) {
			return ERROR_VIEW;
		}

		// 判断该条信息是否可以被录入
		Boolean canSave = shuaService.judgeSave(shua);
		if (!canSave) {
			addFlashMessage(redirectAttributes, Message.error("规定期限内不能刷！"));
			return "redirect:list.jhtml";
		}
		shuaService.save(shua);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml";
	}

	/**
	 * 编辑
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(Long id, ModelMap model) {

		model.addAttribute("shua", shuaService.find(id));
		return "/admin/shua/edit";
	}

	/**
	 * 更新
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(ShuaItem shua, RedirectAttributes redirectAttributes) {
		if (!isValid(shua)) {
			return ERROR_VIEW;
		}
		// 判断是否有权限修改
		if (!shuaService.getEditPermission()) {
			addFlashMessage(redirectAttributes, Message.error("您没有权限修改！"));
			return "redirect:list.jhtml";
		}
		
		shuaService.update(shua);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml";
	}

	/**
	 * 逻辑删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Message delete(Long[] ids) {
		// 判断是否有权限删除
		if (!shuaService.getEditPermission()) {
			return Message.error("您没有权限修改！");
		}
		ShuaItem shua = new ShuaItem();
		for (Long id : ids) {
			shua.setId(id);
			shua.setStatus(AccountsConstant.STATUS_SHUADAN_DELETED);
			shuaService.update(shua, "operator", "fromNick", "keyword", "name", "platform", "qq", "storeId", "sum", "target", "toNick", "xiaohao");
		}

		return SUCCESS_MESSAGE;
	}

}
